package cn.csc.framework.ip.core.utils;

import cn.csc.framework.ip.core.CompanyCode;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 快递100快递公司标准编码
 * @author liuchuang
 * @version 1.0
 * @modify
 * @since 2025/7/9/0009 14:10
 */
@Slf4j
public class KuaiDi100CompanyCodeUtils {

    private final static KuaiDi100CompanyCodeUtils INSTANCE = new KuaiDi100CompanyCodeUtils();


    /**
     * companyCodes 内存缓存，提升访问速度
     */
    private static Map<String, CompanyCode> companyCodeMapByName;

    private static Map<String, CompanyCode> companyCodeSimpleMapByName;

    private static Map<String, CompanyCode> companyCodeMapByCode;


    private KuaiDi100CompanyCodeUtils(){
        long now = System.currentTimeMillis();
        companyCodeMapByName = new HashMap<>();
        companyCodeSimpleMapByName = new HashMap<>();
        companyCodeMapByCode = new HashMap<>();
        // 从 csv 中加载数据
        List<CsvRow> rows = CsvUtil.getReader().read(ResourceUtil.getUtf8Reader("kuaidi100-company-code.csv")).getRows();
        rows.remove(0); // 删除 header
        for (CsvRow row : rows) {
            // 创建 CompanyCode 对象
            CompanyCode code = new CompanyCode(Integer.valueOf(row.get(0)), row.get(1), row.get(2), row.get(3));
            // 添加到 companyCodes 中
            companyCodeMapByName.put(code.getName(), code);
            companyCodeMapByCode.put(code.getCode(), code);
        }
        rows.stream().limit(20).forEach(r ->{
            // 创建 CompanyCode 对象
            CompanyCode code = new CompanyCode(Integer.valueOf(r.get(0)), r.get(1), r.get(2), r.get(3));
            // 添加到 companyCodes 中
            companyCodeSimpleMapByName.put(code.getName(), code);
        });
        log.info("启动加载 KuaiDi100CompanyCodeUtils 成功，耗时 ({}) 毫秒", System.currentTimeMillis() - now);
    }

    /**
     * 获得指定公司名称对应的数据
     *
     * @param name 公司名字
     */
    public static List<CompanyCode> getByName(String name) {
        if (companyCodeMapByName.containsKey(name)){
            return Arrays.asList(companyCodeMapByName.get(name));
        }
        if (name == null || name.isEmpty()){
            return new ArrayList<>(companyCodeMapByName.values());
        }

        return companyCodeMapByName.entrySet().stream().filter(code -> code.getKey().indexOf(name) > 0).map(Map.Entry::getValue).collect(Collectors.toList());
    }

    public static List<CompanyCode> getSimpleByName(String name) {
        if (companyCodeSimpleMapByName.containsKey(name)){
            return Arrays.asList(companyCodeSimpleMapByName.get(name));
        }
        if (name == null || name.isEmpty()){
            return new ArrayList<>(companyCodeSimpleMapByName.values());
        }

        return companyCodeSimpleMapByName.entrySet().stream().filter(code -> code.getKey().indexOf(name) > 0).map(Map.Entry::getValue).collect(Collectors.toList());
    }

    /**
     * 获得指定公司编码对应的数据
     *
     * @param code 公司编码
     */
    public static CompanyCode getByCode(String code) {
        return companyCodeMapByCode.get(code);
    }



}
